
 tempfile main
 save `main'
	
	*******************************************************
	* (2.1) First births
	*		Generate the life table predictions using 
	*		the pre LLF sample
	*******************************************************
	drop if current_age<age_marriage
	use `main', clear 
	

	local n = 1
	local ey = -5
	forval age = 15/49 { 
	
			sum delivered if current_age == `age' & years_since_LLF==`ey' & parity==0
				local xb_0 = r(mean)
			sum delivered if current_age == `age' & years_since_LLF==`ey' & parity==1
				local xb_1 = r(mean)
			sum delivered if current_age == `age' & years_since_LLF==`ey' & parity==2
				local xb_2 = r(mean)
			sum delivered if current_age == `age' & years_since_LLF==`ey' & parity==3
				local xb_3 = r(mean)
			sum delivered if current_age == `age' & years_since_LLF==`ey' & parity==4
				local xb_4 = r(mean)
			sum delivered if current_age == `age' & years_since_LLF==`ey' & parity==5
				local xb_5 = r(mean)
			sum delivered if current_age == `age' & years_since_LLF==`ey' & parity==6
				local xb_6 = r(mean)
				
				if `age' == 15 {
					mat Qx_`n' = -5, `age', `xb_0', `xb_1', `xb_2', `xb_3', `xb_4', `xb_5', `xb_6'
					} 
				else if `age' > 15 { 
					mat temp = -5, `age', `xb_0', `xb_1', `xb_2', `xb_3', `xb_4', `xb_5', `xb_6'
					mat Qx_`n' = Qx_`n' \ temp
					}
		}
		
	local n = 2
	local ey = -4
	forval age = 15/49 { 
	
			sum delivered if current_age == `age' & years_since_LLF==`ey' & parity==0
				local xb_0 = r(mean)
			sum delivered if current_age == `age' & years_since_LLF==`ey' & parity==1
				local xb_1 = r(mean)
			sum delivered if current_age == `age' & years_since_LLF==`ey' & parity==2
				local xb_2 = r(mean)
			sum delivered if current_age == `age' & years_since_LLF==`ey' & parity==3
				local xb_3 = r(mean)
			sum delivered if current_age == `age' & years_since_LLF==`ey' & parity==4
				local xb_4 = r(mean)
			sum delivered if current_age == `age' & years_since_LLF==`ey' & parity==5
				local xb_5 = r(mean)
			sum delivered if current_age == `age' & years_since_LLF==`ey' & parity==6
				local xb_6 = r(mean)
				
				if `age' == 15 {
					mat Qx_`n' = -5, `age', `xb_0', `xb_1', `xb_2', `xb_3', `xb_4', `xb_5', `xb_6'
					} 
				else if `age' > 15 { 
					mat temp = -5, `age', `xb_0', `xb_1', `xb_2', `xb_3', `xb_4', `xb_5', `xb_6'
					mat Qx_`n' = Qx_`n' \ temp
					}
		}		

	local n = 3
	local ey = -3
	forval age = 15/49 { 
	
			sum delivered if current_age == `age' & years_since_LLF==`ey' & parity==0
				local xb_0 = r(mean)
			sum delivered if current_age == `age' & years_since_LLF==`ey' & parity==1
				local xb_1 = r(mean)
			sum delivered if current_age == `age' & years_since_LLF==`ey' & parity==2
				local xb_2 = r(mean)
			sum delivered if current_age == `age' & years_since_LLF==`ey' & parity==3
				local xb_3 = r(mean)
			sum delivered if current_age == `age' & years_since_LLF==`ey' & parity==4
				local xb_4 = r(mean)
			sum delivered if current_age == `age' & years_since_LLF==`ey' & parity==5
				local xb_5 = r(mean)
			sum delivered if current_age == `age' & years_since_LLF==`ey' & parity==6
				local xb_6 = r(mean)
				
				if `age' == 15 {
					mat Qx_`n' = -5, `age', `xb_0', `xb_1', `xb_2', `xb_3', `xb_4', `xb_5', `xb_6'
					} 
				else if `age' > 15 { 
					mat temp = -5, `age', `xb_0', `xb_1', `xb_2', `xb_3', `xb_4', `xb_5', `xb_6'
					mat Qx_`n' = Qx_`n' \ temp
					}
		}			

	local n = 4
	local ey = -2
	forval age = 15/49 { 
	
			sum delivered if current_age == `age' & years_since_LLF==`ey' & parity==0
				local xb_0 = r(mean)
			sum delivered if current_age == `age' & years_since_LLF==`ey' & parity==1
				local xb_1 = r(mean)
			sum delivered if current_age == `age' & years_since_LLF==`ey' & parity==2
				local xb_2 = r(mean)
			sum delivered if current_age == `age' & years_since_LLF==`ey' & parity==3
				local xb_3 = r(mean)
			sum delivered if current_age == `age' & years_since_LLF==`ey' & parity==4
				local xb_4 = r(mean)
			sum delivered if current_age == `age' & years_since_LLF==`ey' & parity==5
				local xb_5 = r(mean)
			sum delivered if current_age == `age' & years_since_LLF==`ey' & parity==6
				local xb_6 = r(mean)
				
				if `age' == 15 {
					mat Qx_`n' = -5, `age', `xb_0', `xb_1', `xb_2', `xb_3', `xb_4', `xb_5', `xb_6'
					} 
				else if `age' > 15 { 
					mat temp = -5, `age', `xb_0', `xb_1', `xb_2', `xb_3', `xb_4', `xb_5', `xb_6'
					mat Qx_`n' = Qx_`n' \ temp
					}
		}			
		

	local n = 5
	local ey = -1
	forval age = 15/49 { 
	
			sum delivered if current_age == `age' & years_since_LLF==`ey' & parity==0
				local xb_0 = r(mean)
			sum delivered if current_age == `age' & years_since_LLF==`ey' & parity==1
				local xb_1 = r(mean)
			sum delivered if current_age == `age' & years_since_LLF==`ey' & parity==2
				local xb_2 = r(mean)
			sum delivered if current_age == `age' & years_since_LLF==`ey' & parity==3
				local xb_3 = r(mean)
			sum delivered if current_age == `age' & years_since_LLF==`ey' & parity==4
				local xb_4 = r(mean)
			sum delivered if current_age == `age' & years_since_LLF==`ey' & parity==5
				local xb_5 = r(mean)
			sum delivered if current_age == `age' & years_since_LLF==`ey' & parity==6
				local xb_6 = r(mean)
				
				if `age' == 15 {
					mat Qx_`n' = -5, `age', `xb_0', `xb_1', `xb_2', `xb_3', `xb_4', `xb_5', `xb_6'
					} 
				else if `age' > 15 { 
					mat temp = -5, `age', `xb_0', `xb_1', `xb_2', `xb_3', `xb_4', `xb_5', `xb_6'
					mat Qx_`n' = Qx_`n' \ temp
					}
		}			


	local n = 6
	local ey = 0
	forval age = 15/49 { 
	
			sum delivered if current_age == `age' & years_since_LLF==`ey' & parity==0
				local xb_0 = r(mean)
			sum delivered if current_age == `age' & years_since_LLF==`ey' & parity==1
				local xb_1 = r(mean)
			sum delivered if current_age == `age' & years_since_LLF==`ey' & parity==2
				local xb_2 = r(mean)
			sum delivered if current_age == `age' & years_since_LLF==`ey' & parity==3
				local xb_3 = r(mean)
			sum delivered if current_age == `age' & years_since_LLF==`ey' & parity==4
				local xb_4 = r(mean)
			sum delivered if current_age == `age' & years_since_LLF==`ey' & parity==5
				local xb_5 = r(mean)
			sum delivered if current_age == `age' & years_since_LLF==`ey' & parity==6
				local xb_6 = r(mean)
				
				if `age' == 15 {
					mat Qx_`n' = -5, `age', `xb_0', `xb_1', `xb_2', `xb_3', `xb_4', `xb_5', `xb_6'
					} 
				else if `age' > 15 { 
					mat temp = -5, `age', `xb_0', `xb_1', `xb_2', `xb_3', `xb_4', `xb_5', `xb_6'
					mat Qx_`n' = Qx_`n' \ temp
					}
		}	
		
	forval n = 7/14 {
	local ey = `n'-6
	forval age = 15/49 { 
	
			sum delivered if current_age == `age' & years_since_LLF==`ey' & parity==0
				local xb_0 = r(mean)
			sum delivered if current_age == `age' & years_since_LLF==`ey' & parity==1
				local xb_1 = r(mean)
			sum delivered if current_age == `age' & years_since_LLF==`ey' & parity==2
				local xb_2 = r(mean)
			sum delivered if current_age == `age' & years_since_LLF==`ey' & parity==3
				local xb_3 = r(mean)
			sum delivered if current_age == `age' & years_since_LLF==`ey' & parity==4
				local xb_4 = r(mean)
			sum delivered if current_age == `age' & years_since_LLF==`ey' & parity==5
				local xb_5 = r(mean)
			sum delivered if current_age == `age' & years_since_LLF==`ey' & parity==6
				local xb_6 = r(mean)
				
				if `age' == 15 {
					mat Qx_`n' = -5, `age', `xb_0', `xb_1', `xb_2', `xb_3', `xb_4', `xb_5', `xb_6'
					} 
				else if `age' > 15 { 
					mat temp = -5, `age', `xb_0', `xb_1', `xb_2', `xb_3', `xb_4', `xb_5', `xb_6'
					mat Qx_`n' = Qx_`n' \ temp
					}
		}	
	}	
		
	forval ey = 1/14 { 
		forval row = 1/35 { 
			di "EY `ey' Row `row'"
			forval col = 3/9 {
				if Qx_`ey'[`row', `col'] == . { 
					matrix Qx_`ey'[`row', `col'] = 0 
					}
				}
			}
		}
		
	forval i = 1/14 { 	
		local eventyear = `i' - 6 
		mat Lx_`i' = `eventyear', 15, 1

		scalar dx = Qx_`i'[1, 3]*Lx_`i'[1, 3] 
		mat Dx_`i' = `eventyear', 15, dx 
	
		forval j = 2/35 { 
			local age = `j' + 14
			local n_1 = `j' - 1
	
			scalar lx = Lx_`i'[`n_1', 3] - Dx_`i'[`n_1', 3] 
			mat temp = `eventyear', `age', lx
			mat Lx_`i' = Lx_`i' \ temp
	
			scalar dx = Qx_`i'[`j', 3]*Lx_`i'[`j', 3] 
			mat temp = `eventyear', `age' , dx 
			mat Dx_`i' = Dx_`i' \ temp	
			}	
		}
 
		
		forval parity = 1/6 { 
		forval i = 1/14 { 
		
			local col_p = `parity' + 3
			local col_p_1 = `col_p' - 1
		
			local r = rowsof(Lx_`i')
			mat temp = J(`r', 1, .)
		
			mat Lx_`i' = Lx_`i', temp 
			mat Dx_`i' = Dx_`i', temp 
		
			mat Lx_`i'[1, `col_p'] =  0
			mat Dx_`i'[1, `col_p'] = Qx_`i'[1, `col_p']*(Lx_`i'[1, `col_p']+(Dx_`i'[1, `col_p_1']/2)) 
		
			forval n = 2/35 { 
				local n_1 = `n' - 1
		
				mat Lx_`i'[`n', `col_p'] = Lx_`i'[`n_1', `col_p'] + Dx_`i'[`n_1', `col_p_1'] - Dx_`i'[`n_1', `col_p'] 
				mat Dx_`i'[`n', `col_p'] = Qx_`i'[`n', `col_p']*(Lx_`i'[`n', `col_p']+(Dx_`i'[`n', `col_p_1']/2))	
				}	
			}
		}

*******************************************************
* (3.0) Output key life table components
*******************************************************

	forval i = 1/14 { 
		mat colnames Qx_`i' = "Eventyear" "Age" "Qx_1" "Qx_2" "Qx_3" "Qx_4" "Qx_5" "Qx_6" "Qx_7"
		mat colnames Lx_`i' = "Eventyear" "Age" "Lx_1" "Lx_2" "Lx_3" "Lx_4" "Lx_5" "Lx_6" "Lx_7"
		mat colnames Dx_`i' = "Eventyear" "Age" "Dx_1" "Dx_2" "Dx_3" "Dx_4" "Dx_5" "Dx_6" "Dx_7"
		}
				
	preserve 		
	forval i = 1/14 { 
		clear 
		svmat Dx_`i', names(col) 
			gen ASFR = Dx_1 + Dx_2 + Dx_3 + Dx_4 + Dx_5 + Dx_6 + Dx_7

				
			count 
			local max = r(N) 
			local ey = `i' - 6
			scalar TFR_`i' = ASFR[1]
			forval age = 2/35 { 
				scalar TFR_`i' = TFR_`i' + ASFR[`age']
				}
					
			if `i' == 1 {
				mat TFR = `ey', TFR_`i'
				} 
				
			else if `i' > 1 { 
				mat temp = `ey', TFR_`i'
				mat TFR = TFR \ temp 
				
				} 
	}	
	 
	
		clear 
		svmat TFR
		rename TFR1 eventyear 
		rename TFR2 tfrunadjusted
			save `"$d_data\TFR_undadjusted_eventtime.dta"', replace
	
	restore	
*******************************************************


		